package com.scnu.domain.activity.service.trial.node;

import com.scnu.domain.activity.model.entity.MarketProductEntity;
import com.scnu.domain.activity.model.entity.TrialBalanceEntity;
import com.scnu.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import com.scnu.domain.activity.model.valobj.SkuVO;
import com.scnu.domain.activity.service.discount.IDiscountCalculateService;
import com.scnu.domain.activity.service.trial.AbstractGroupBuyMarketSupport;
import com.scnu.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import com.scnu.domain.activity.service.trial.thread.queryGroupBuyActivityDiscountVOThreadTask;
import com.scnu.domain.activity.service.trial.thread.querySkuVOFromDBThreadTask;
import com.scnu.types.design.framework.tree.StrategyHandler;
import com.scnu.types.enums.ResponseCode;
import com.scnu.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.*;


@Slf4j
@Service
public class MarketNode extends AbstractGroupBuyMarketSupport<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> {

    @Resource
    private ThreadPoolExecutor threadPoolExecutor;

//    @Resource
//    private EndNode endNode;
    @Resource
    private ErrorNode errorNode;
    @Resource
    private TagNode tagNode;

    @Resource
    private Map<String, IDiscountCalculateService> discountCalculateServicesMap;

    @Override
    protected void multiThread(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws ExecutionException, InterruptedException, TimeoutException {
        queryGroupBuyActivityDiscountVOThreadTask queryGroupBuyActivityDiscountVOThreadTask = new queryGroupBuyActivityDiscountVOThreadTask(requestParameter.getChannel(), requestParameter.getSource(), repository,requestParameter.getGoodsId());
        FutureTask<GroupBuyActivityDiscountVO> groupBuyActivityDiscountVOFutureTask = new FutureTask<>(queryGroupBuyActivityDiscountVOThreadTask);
        threadPoolExecutor.execute(groupBuyActivityDiscountVOFutureTask);

        querySkuVOFromDBThreadTask querySkuVOFromDBThreadTask = new querySkuVOFromDBThreadTask(requestParameter.getGoodsId(),repository);
        FutureTask<SkuVO> skuVOFutureTask = new FutureTask<>(querySkuVOFromDBThreadTask);
        threadPoolExecutor.execute(skuVOFutureTask);

        dynamicContext.setGroupBuyActivityDiscountVO(groupBuyActivityDiscountVOFutureTask.get(timeout, TimeUnit.MINUTES));
        dynamicContext.setSkuVO(skuVOFutureTask.get(timeout, TimeUnit.MINUTES));
    }

    @Override
    public TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {

        GroupBuyActivityDiscountVO groupBuyActivityDiscountVO = dynamicContext.getGroupBuyActivityDiscountVO();
        if (groupBuyActivityDiscountVO == null) {return router(requestParameter, dynamicContext);}
        GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount = groupBuyActivityDiscountVO.getGroupBuyDiscount();

        SkuVO skuVO = dynamicContext.getSkuVO();
        if (groupBuyDiscount == null || skuVO == null) {return router(requestParameter, dynamicContext);}

        // 拼团优惠试算
        IDiscountCalculateService discountCalculateService = discountCalculateServicesMap.get(groupBuyDiscount.getMarketPlan());
        if (discountCalculateService == null) {
            throw new AppException(ResponseCode.E0001.getCode(),ResponseCode.E0001.getInfo());
        }

        BigDecimal deductionPrice = discountCalculateService.calculate(requestParameter.getUserId(), skuVO.getOriginalPrice(), groupBuyDiscount);
        dynamicContext.setDeductionPrice(deductionPrice);

        return router(requestParameter, dynamicContext);
    }

    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) {

        if (dynamicContext.getGroupBuyActivityDiscountVO() == null
        || dynamicContext.getSkuVO() == null
        || dynamicContext.getDeductionPrice() == null) {
            return errorNode;
        }

        return tagNode;
    }
}
